// pages/batchAddConfigList/batchAddConfigList.js
var common = require('../../utils/common.js');
var that = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    furnitureData:[],
  },
  //删除
  deleteItem: function (e) {
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确定要删除配置' + (index + 1),
      success: function (res) {
        if (res.confirm) {

          //删除对象
          that.data.listData.splice(index, 1);
          that.setData({
            listData: that.data.listData
          })
          wx.setStorageSync('listConfig', that.data.listData);
          //删除应用到的房号
          var house = wx.getStorageSync('rooms');
          if (house) {
            for (var i in house.floors) {
              for (var j in house.floors[i].rooms) {
                if (house.floors[i].rooms[j].config == that.data.configIndex) {
                  house.floors[i].rooms[j].config = -1;
                } else if (house.floors[i].rooms[j].config > that.data.configIndex) {
                  house.floors[i].rooms[j].config--
                }
              }
            }
            wx.setStorageSync('rooms', house)
          }

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  submit: function () {

    var house = wx.getStorageSync('rooms');
    var listConfig = this.data.listData;
    console.log(house);
    console.log(listConfig);
    if (house) {
      for (var i in house.floors) {
        delete house.floors[i].check
        for (var j in house.floors[i].rooms) {
          if (house.floors[i].rooms[j].config < listConfig.length && house.floors[i].rooms[j].config != -1) {
            // if (house.floors[i].rooms[j].config) {

            var config = listConfig[house.floors[i].rooms[j].config];
            console.log(house.floors[i].rooms[j].config)
            console.log('config')
            console.log(config)
            house.floors[i].rooms[j].area = config.area
            house.floors[i].rooms[j].price = config.price
            house.floors[i].rooms[j].chargemode = config.payTypeKey
            house.floors[i].rooms[j].furniture = JSON.stringify(config.roomsConf);
            house.floors[i].rooms[j].houseType = config.roomTypeKey
            // }
          }
          delete house.floors[i].rooms[j].check
          delete house.floors[i].rooms[j].config

        }

      }
      console.log(house)
      //提交
      console.log(that.data.houseId)
      common._post_json('/wx/house/save', house,
        //成功
        function (res) {
          console.log(res.data);
          if (res.data.success) {
            wx.showModal({
              title: '提示',
              content: '保存成功',
            })
            wx.setStorageSync('result', 'result')
            wx.switchTab({
              url: '../house/house'
            })

          }
        });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    that = this;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var config = wx.getStorageSync('config')
    wx.removeStorageSync('config')
    console.log(config)
    if (config) {
      if (this.data.listData.length > 0) {
        if (config.configIndex >= 0 && config.configIndex < this.data.listData.length) {
          this.data.listData[config.configIndex] = config;
        } else {
          this.data.listData.push(config);
        }
      } else {
        this.data.listData.push(config);
      }
      this.setData({
        listData: this.data.listData
      })
      wx.setStorageSync('listConfig', this.data.listData);
      console.log('this.data.listData');
      console.log(this.data.listData);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})